package control;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.service.MemberService;
import model.vo.MemberVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;


public class MemberController extends MultiActionController{
	private MemberService memberService;
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public ModelAndView registerMember
	(HttpServletRequest request,HttpServletResponse response) throws Exception{

		ModelAndView mv=new ModelAndView();
//#id#,#password#,#name#,#ssn#,#address#,#tel#,#email#,#member_type
		String id=request.getParameter("id");
		String password=request.getParameter("password");
		String name=request.getParameter("name");
		String ssn=request.getParameter("ssn");
		String address=request.getParameter("address");
		String tel=request.getParameter("tel");
		String email=request.getParameter("email");
		String member_type=request.getParameter("member_type");
		MemberVO vo=new MemberVO(id,password,name,ssn,address,tel,email,member_type);
		System.out.println(vo);
		try{
			mv.setViewName("register_result");
			memberService.registerMember(vo);
		}catch(Exception e){
			mv.addObject("info",e.getMessage());
		}
		return mv;	
	}
	public ModelAndView loginMember
	(HttpServletRequest request,HttpServletResponse response) throws Exception{

		ModelAndView mv=new ModelAndView();
		String id=request.getParameter("id");
		String password=request.getParameter("password");
		System.out.println(id+" " +password);
		try{
			mv.setViewName("login_result");
			MemberVO vo=memberService.loginMember(id, password);
			System.out.println(vo);
			HttpSession session=request.getSession();
			session.setAttribute("member", vo);
		}catch(Exception e){
			mv.addObject("info",e.getMessage());
		}
		return mv;	
	}
	public ModelAndView logout
	(HttpServletRequest request,HttpServletResponse response) throws Exception{

		ModelAndView mv=new ModelAndView();
		try{
			mv.setViewName("logout");
			HttpSession session=request.getSession(false);
			if(session!=null)
				session.invalidate();
		}catch(Exception e){
			mv.addObject("info",e.getMessage());
		}
		return mv;	
	}
	//updateMember
	public ModelAndView updateMember
	(HttpServletRequest request,HttpServletResponse response) throws Exception{

		ModelAndView mv=new ModelAndView();
		String id=request.getParameter("id");
		String password=request.getParameter("password");
		String address=request.getParameter("address");
		String tel=request.getParameter("tel");
		String email=request.getParameter("email");
		MemberVO vo=new MemberVO();
		vo.setId(id);	vo.setPassword(password);	vo.setAddress(address);
		vo.setTel(tel);	vo.setEmail(email);
		try{
			mv.setViewName("update_result");
			MemberVO updateVo=memberService.updateMember(vo);
			HttpSession session=request.getSession();
			session.setAttribute("member", updateVo);
		}catch(Exception e){
			mv.addObject("info",e.getMessage());
		}
		return mv;	
	}
	//isExist
	public ModelAndView IdCheck
	(HttpServletRequest request,HttpServletResponse response) throws Exception{

		ModelAndView mv=new ModelAndView();
		String id=request.getParameter("id");
		System.out.println("eeeeeeeeeeeeeee");
		try{
			mv.setViewName("idCheck");
			boolean flag=memberService.idCheck(id);
			request.setAttribute("idflag", flag);
		}catch(Exception e){
			mv.addObject("info",e.getMessage());
		}
		return mv;	
	}
}
